Retropie-stuff: game controller

Retropie: runcommand-onstart.sh

#!/usr/bin/env bash


echo

"start:"

$

1

>> /dev/serial0

Retropie: runcommand-onend.sh

#!/usr/bin/env bash


echo

"stop:"

$

1

>> /dev/serial0

mBed 24 channel 12bit PWM LED-driver: TLC5947_SPI.h

Library is based on the Arduino library, but instead of big-banging it is rewritten to be used with SPI on a mBed 1768.

#ifndef _TLC5947_SPI_H_
#define _TLC5947_SPI_H_

#include

"mbed.h"



#define OUT_NONE  0x00
#define OUT_LAT   0x01
#define OUT_OE    0x02
#define OUT_ALL   (OUT_LAT | OUT_OE)



////////////////////////////////////////////////////////////////////////////////
// class TLC5947_Spi

/** TLC5947 with SPI class
* Used for driving the Adafruit 24-Channel 12 bit PWM LED Driver
*/


class

TLC5947_Spi


{

public

:


    

/** Creating TLC5947_Spi instance
    * @param _pi Reference to its SPI to use
    * @param Lat Update-pin to write hardware pwm values (on rising edge)
    * @param OE
    */


    

TLC5947_Spi(SPI& _pi, PinName Lat, PinName OE);


    
    

/** Initializes the class, including the Spi communication
    *
    */


    

void

    init(

void

);


    
    

/** Set the PWM-value per channel
    * @param nChannel Selects the channel to be set (0..23)
    * @param nPwm Pwm value to set
    */


    

void

    setPWM(

uint16_t

nChannel,

uint16_t

nPwm);


    
    

/** Set the RGB-led based on the led number
    * Connect RGB-leds in the appropriate order R, G, B and
    * start at the channel 0
    * @param nLed Led-number to be set (0..5)
    * @param red Red value for the RGB-led
    * @param green Green value for the RGB-led
    * @param blue Blue value for the RGB-led
    */


    

void

    setLED(

uint16_t

nLed,

uint16_t

red,

uint16_t

green,

uint16_t

blue);


    
    

/** Updates the pwm values to the hardware (only changed values)
    */


    

void

    update(

void

);


    
    

/** Updates all the pwm values to the hardware (writes all channels)
    */


    

void

    write(

void

);


    

protected

:



private

:


    

SPI&        m_spi;


    

BusInOut    m_bIO;


    

uint16_t

    m_pwmBuffer[

24

];


    

uint16_t

    m_pwmSend[

24

];


};



#endif //_TLC5947_SPI_H_


mBed 24 channel 12bit PWM LED-driver: TLC5947_SPI.cpp

#include

"TLC5947_SPI.h"



////////////////////////////////////////////////////////////////////////////////
// class TLC5947_Spi

//******************************************************************************


TLC5947_Spi::TLC5947_Spi(SPI& spi, PinName Lat, PinName OE) : m_spi(spi), m_bIO(Lat, OE)
{
    memset(m_pwmBuffer,

0

,

sizeof

(m_pwmBuffer));
    memset(m_pwmSend,

0

,

sizeof

(m_pwmSend));
}



//******************************************************************************


void

TLC5947_Spi::init(

void

)
{
    m_bIO.output();
    m_bIO.mode(PullDown);
    m_bIO.write(OUT_NONE);
    m_spi.format(

12

);
    m_spi.frequency(

10000000

);
}



//******************************************************************************


void

TLC5947_Spi::update(

void

)
{
    m_bIO.write(OUT_NONE);


    
        

for

(

int16_t

nChannel =

24

-

1

; nChannel >=

0

; nChannel--)
    {


                

if

(m_pwmBuffer[nChannel] == m_pwmSend[nChannel])


                    

continue

;
        
        m_spi.write(m_pwmBuffer[nChannel]);
    }
    
    memcpy(m_pwmSend, m_pwmBuffer,

sizeof

(m_pwmBuffer));
    
    m_bIO.write(OUT_LAT);
    m_bIO.write(OUT_NONE);
}



//******************************************************************************


void

TLC5947_Spi::write(

void

)
{
    m_bIO.write(OUT_NONE);



        

for

(

int16_t

nChannel =

24

-

1

; nChannel >=

0

; nChannel--)
        m_spi.write(m_pwmBuffer[nChannel]);
    
    memcpy(m_pwmSend, m_pwmBuffer,

sizeof

(m_pwmBuffer));
    
    m_bIO.write(OUT_LAT);
    m_bIO.write(OUT_NONE);
}



//******************************************************************************


void

TLC5947_Spi::setPWM(

uint16_t

nChannel,

uint16_t

nPwm)
{


        

if

(nPwm >

4095

)
        nPwm =

4095

;


        

if

(nChannel >

24

)


                

return

;
    
    m_pwmBuffer[nChannel] = nPwm;
}



//******************************************************************************


void

TLC5947_Spi::setLED(

uint16_t

nLed,

uint16_t

red,

uint16_t

green,

uint16_t

blue)
{
    setPWM(nLed *

3

, red);
    setPWM(nLed *

3

+

1

, green);
    setPWM(nLed *

3

+

2

, blue);
}